package com.mcc.quartz.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.quartz.domain.SysJobLog;
import com.mcc.quartz.mapper.SysJobLogMapper;
import com.mcc.quartz.service.ISysJobLogService;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author gxy
 */
@Service
public class SysJobLogServiceImpl implements ISysJobLogService {

    @Autowired
    private SysJobLogMapper jobLogMapper;

    /**
     * 获取quartz调度器日志的计划任务
     *
     * @param jobLog 调度日志信息
     * @return 调度任务日志集合
     */
    @Override
    public List<SysJobLog> selectJobLogList(SysJobLog jobLog) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(jobLog)) {
            queryWrapper.like(ObjectUtil.isNotEmpty(jobLog.getJobName()), "job_name", jobLog.getJobName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(jobLog.getJobGroup()), "job_group", jobLog.getJobGroup());
            queryWrapper.eq(ObjectUtil.isNotEmpty(jobLog.getStatus()), "status", jobLog.getStatus());
            queryWrapper.like(ObjectUtil.isNotEmpty(jobLog.getInvokeTarget()), "invoke_target", jobLog.getInvokeTarget());
            queryWrapper.ge(ObjectUtil.isNotEmpty(jobLog.getBeginTime()), "date_format(create_time,'%Y-%m-%d')", jobLog.getBeginTime());
            queryWrapper.le(ObjectUtil.isNotEmpty(jobLog.getEndTime()), "date_format(create_time,'%Y-%m-%d')", jobLog.getEndTime());
        }
        return jobLogMapper.selectList(queryWrapper);
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    @Override
    public SysJobLog selectJobLogById(Long jobLogId) {
        return jobLogMapper.selectById(jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    @Override
    public void addJobLog(SysJobLog jobLog) {
        jobLog.setCreateTime(new Date());
        jobLogMapper.insert(jobLog);
    }

    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJobLogByIds(Long[] logIds) {
        if (logIds.length == 0) return 0;
        return jobLogMapper.deleteBatchIds(ListUtil.toList(logIds));
    }

    /**
     * 删除任务日志
     *
     * @param jobId 调度日志ID
     */
    @Override
    public int deleteJobLogById(Long jobId) {
        return jobLogMapper.deleteById(jobId);
    }

    /**
     * 清空任务日志
     */
    @Override
    public void cleanJobLog() {
        jobLogMapper.cleanJobLog();
    }
}
